package com.seu.multithread.demo04;

/**
 * @author liangfeihu
 * @since 2019/8/16
 */
public class PrintDemoTest {

    private static final Object lock = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(new PrintRunOne(), "线程1");
        Thread t2 = new Thread(new PrintRunTwo(), "线程2");

        t1.start();
        t2.start();

    }

    static class PrintRunOne implements Runnable {
        @Override
        public void run() {
            synchronized (lock) {
                for (int i = 0; i <=10; i+=2) {
                    System.out.println(i + " : " +  Thread.currentThread().getName());
                    lock.notify();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    static class PrintRunTwo implements Runnable {
        @Override
        public void run() {
            synchronized (lock) {
                for (int i = 1; i <=9; i+=2) {
                    System.out.println(i + " : " +  Thread.currentThread().getName());
                    lock.notify();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
